package middleware.llvm.instruction;

import middleware.llvm.value.IRType;
import middleware.llvm.value.Value;

import java.io.PrintStream;

/**
 * 存储指令，将数据存储到内存中
 *
 * @author 杨凯伟
 * @date 2024/10/30
 */
public class StoreInstruction extends Instruction {
    Value value;
    Value ptr;

    public void setValue(Value value) {
        this.value = value;
    }

    public void setPtr(Value ptr) {
        this.ptr = ptr;
    }

    public Value getValue() {
        return value;
    }

    public Value getPtr() {
        return ptr;
    }

    public StoreInstruction(Value value, Value ptr) {
        super(IRType.getVoid(), value, ptr);
        this.value = value;
        this.ptr = ptr;
    }

    @Override
    public void replaceOperand(int pos, Value newOperand) {
        super.replaceOperand(pos, newOperand);
        switch (pos) {
            case 0 -> value = newOperand;
            case 1 -> ptr = newOperand;
        }
    }

    @Override
    public void dump(PrintStream out) {
        out.printf("  store %s, %s\n", value.toString(), ptr.toString());
    }
}
